#include"Manager.h"
using namespace std;
#include<fstream>
#include"globalFile.h"
#include<unistd.h>
#include<algorithm>






Manager::Manager(std::string m_Account,std::string m_name, 
                std::string m_Pwd)
{
    this->m_Account=m_Account;
    this->m_Name=m_name;
    this->m_Pwd=m_Pwd;  

    // 初始化容器
    this->initVector();

    ifstream ifc;
    ifc.open(COMPUERT_FILE,ios::in);

    ComputerRoom computerRoom;

    while(ifc>>computerRoom.m_RoomID && ifc>>computerRoom.m_MaxCapatity)
    {
        vCom.push_back(computerRoom);
    }
    cout<<"机房的数量为："<<vCom.size()<<endl;

    ifc.close();






};

void Manager::openMenu()
{
    cout << "欢迎管理员："<<this->m_Name << "登录！" << endl;
	cout << "\t\t ---------------------------------\n";
	cout << "\t\t|                                |\n";
	cout << "\t\t|          1.添加账号            |\n";
	cout << "\t\t|                                |\n";
	cout << "\t\t|          2.查看账号            |\n";
	cout << "\t\t|                                |\n";
	cout << "\t\t|          3.查看机房            |\n";
	cout << "\t\t|                                |\n";
	cout << "\t\t|          4.清空预约            |\n";
	cout << "\t\t|                                |\n";
	cout << "\t\t|          0.注销登录            |\n";
	cout << "\t\t|                                |\n";
	cout << "\t\t ---------------------------------\n";
	cout << "请选择您的操作： " << endl;

};

void Manager::addAccount()
{
    cout<<"请输入添加帐号类型\n";
    cout<<"1.添加学生\n";
    cout<<"2.添加老师\n";

    string filename;
    string tip;
    ofstream ofs;

    int select=0;
    cin>>select;
    if (select==1)
    {
        filename=STUDENT_FILE;
        tip="请输入学号s";
    }
    else 
    {
        filename=TEACHER_FILE;
        tip="请输入教师号t";

    }

    ofs.open(filename,ios::out |ios::app);

    string account;
    string name;
    string pwd;
    cout<<tip<<endl;

    string errorTip;
    errorTip="帐号重复请重新输入\n";

    cin>>account;

    while(checkRepert(account,select))
    {
        cout<<errorTip;
        cin>>account;
    }


    cout<<"请输入姓名:\n";
    cin>>name;
    cout<<"请输入密码:\n";
    cin>>pwd;
    
    ofs<<"\n"<<account<<" "<<name<<" "<<pwd<<" "<<endl;
    ofs.close();

    cout<<"添加帐号成功\n";

    // 更新菜单
    initVector();
    sleep(1);
    system("clear");
};

void printStudent(Student &student)
{
    cout<<"学号："<<student.m_Account<<"姓名："<<student.m_Name<<"密码：" <<student.m_Pwd<<endl;
}
void printTeacher(Teacher &teacher)
{
    cout<<"学号："<<teacher.m_Account<<"姓名："<<teacher.m_Name<<"密码：" <<teacher.m_Pwd<<endl;
}

void clear()
{
    char buf[1024]={0};
    cout<<"请输入回车建继续......\n";
    fgets(buf,1024,stdin);
    system("clear");
}

void Manager::showPerson()
{
    cout<<"请选择查看的内容\n";
    cout<<"1.查看所有的学生\n";
    cout<<"2.查看所有的老师\n";
    
    int select=0;

    cin>>select;
    if(select=1)
    {
        cout<<"所有学生的信息如下\n";
        for_each(vStu.begin(),vStu.end(),printStudent);
    }
    else if (select=2)
    {
        cout<<"所有教师的信息如下\n";
        for_each(vTea.begin(),vTea.end(),printTeacher);
    }
    
    cout<<"该界面保留5s\n";
    sleep(5);
    system("clear");
    
  

};

void Manager::showComputer()
{   
    cout<<"所有的机房信息如下\n";
    for(auto vc:vCom)
    {
        cout<<"机房编号:"<<vc.m_RoomID<<"机房最大容量:"<<vc.m_MaxCapatity<<endl;
    }

    cout<<"该界面保留5s\n";
    sleep(5);
    system("clear");

};

void Manager::cleanFlie()
{
    ofstream ofo;
    ofo.open(ORDER_FILE,ios::trunc);
    ofo.close();

    cout<<"清空成功\n";
    sleep(1);
    system("clear");

};

void Manager::initVector()
{
    ifstream ifs;

    vStu.clear();
    vTea.clear();

    ifs.open(STUDENT_FILE,ios::in);
    if(!ifs.is_open())
    {
        cout<<"无法打开学生文件\n";
        return;
    }

    Student s;
    while(ifs>>s.m_Account && ifs>>s.m_Name &&ifs>>s.m_Pwd)
    {
        vStu.push_back(s);
    }
    cout<<"当前学生帐号的数量为："<<vStu.size()<<endl;
    ifs.close();

    ifs.open(TEACHER_FILE,ios::in);
    if(!ifs.is_open())
    {
        cout<<"无法打开老师文件\n";
        return;
    }

    Teacher t;
    while(ifs>>t.m_Account && ifs>>t.m_Name &&ifs>>t.m_Pwd)
    {
        vTea.push_back(t);
    }
    cout<<"当前老师帐号的数量为："<<vTea.size()<<endl;
    ifs.close();

};

bool Manager::checkRepert(std::string account ,int type)
{
    if (type ==1)
    {
        for (auto student:vStu)
        {
            if(account==student.m_Account)
            {
                return true;
            }
        }
    }
    else
    {
        for (auto teacher:vTea)
        {
            if(account==teacher.m_Account)
            {
                return true;
            }
        }
    }
    return false;

};

